<template>
  <div>
    <el-tag v-for="user in roleUsers" :key="user.id" closable>{{user.name}}</el-tag>
    <el-select v-model="user" filterable remote placeholder="请输入关键词" :remote-method="remoteMethod" @change="handleInputConfirm">
      <el-option v-for="item in users" :key="item.id" :label="item.login" :value="item"></el-option>
    </el-select>
  </div>
</template>
<script>
export default {
  props: {
    appsDisabled: {
      type: Boolean,
      default: function() {
        return true;
      }
    },
    role: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      inputVisible: false,
      roleUsers: [
        {
          id: 1,
          name: "ddd"
        },
        {
          id: 2,
          name: "dddddd"
        }
      ],
      users: [],
      user: {}
    };
  },
  methods: {
    remoteMethod(query) {
      this.$http({
        url: "/uaa/api/users/search",
        method: "GET",
        params: { content: query }
      }).then(resp => {
        this.users = resp.data.content;
      });
    },
    handleInputConfirm(data) {
      console.debug(data);
      this.$http({
        url: "/core/api/secu-role-users",
        method: "POST",
        data: { roleId: this.role.id, userId: data.id }
      }).then(() => {
        this.roleUsers.push({
          id: data.id,
          name: data.login
        });
        this.user = {};
        this.users = [];
        this.$message({
          type: "success",
          message: "成功分配用户!"
        });
      });
    }
  }
};
</script>
<style lang="less" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
